home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / system-config-printer / my-default-printer.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-10-28  |  8KB  |  252 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import cups
  5. import gobject
  6. import gtk
  7. import os
  8. import signal
  9. import sys
  10. domain = 'system-config-printer'
  11. import locale
  12.  
  13. try:
  14.     locale.setlocale(locale.LC_ALL, '')
  15. except locale.Error:
  16.     e = None
  17.     os.environ['LC_ALL'] = 'C'
  18.     locale.setlocale(locale.LC_ALL, '')
  19.  
  20. from gettext import gettext as _
  21. import gettext
  22. gettext.textdomain(domain)
  23.  
  24. def handle_sigchld(signum, stack):
  25.     
  26.     try:
  27.         (pid, status) = os.wait()
  28.         exitcode = os.WEXITSTATUS(status)
  29.         if exitcode != 0:
  30.             print 'Child exit status %d' % exitcode
  31.     except OSError:
  32.         pass
  33.  
  34.  
  35. signal.signal(signal.SIGCHLD, handle_sigchld)
  36.  
  37. class Server:
  38.     
  39.     def __init__(self):
  40.         self.cups_connection = cups.Connection()
  41.         
  42.         try:
  43.             lpoptions = os.environ['HOME']
  44.         except KeyError:
  45.             
  46.             try:
  47.                 lpoptions = '/home/' + os.environ['USER']
  48.             except KeyError:
  49.                 lpoptions = None
  50.             except:
  51.                 None<EXCEPTION MATCH>KeyError
  52.             
  53.  
  54.             None<EXCEPTION MATCH>KeyError
  55.  
  56.         if lpoptions:
  57.             lpoptions += '/.cups/lpoptions'
  58.         
  59.         self.lpoptions = lpoptions
  60.  
  61.     
  62.     def clearUserDefault(self):
  63.         if not self.lpoptions:
  64.             return None
  65.         
  66.         try:
  67.             opts = file(self.lpoptions).readlines()
  68.         except IOError:
  69.             self.lpoptions
  70.             self.lpoptions
  71.             return None
  72.  
  73.         for i in range(len(opts)):
  74.             if opts[i].startswith('Default '):
  75.                 opts[i] = 'Dest ' + opts[i][8:]
  76.                 continue
  77.             self.lpoptions
  78.         
  79.         file(self.lpoptions, 'w').writelines(opts)
  80.  
  81.     
  82.     def setUserDefault(self, default):
  83.         pid = os.fork()
  84.         if pid == 0:
  85.             null = file('/dev/null', 'r+')
  86.             f = null.fileno()
  87.             if f != 0:
  88.                 os.dup2(f, 0)
  89.             
  90.             if f != 1:
  91.                 os.dup2(f, 1)
  92.             
  93.             os.execvp('lpoptions', [
  94.                 'lpoptions',
  95.                 '-d',
  96.                 default])
  97.             sys.exit(1)
  98.         
  99.  
  100.     
  101.     def isUserDefaultSet(self):
  102.         if self.getUserDefault() != self.getSystemDefault():
  103.             return True
  104.         return False
  105.  
  106.     
  107.     def getUserDefault(self):
  108.         dests = self.cups_connection.getDests()
  109.         if dests.has_key((None, None)):
  110.             return dests[(None, None)].name
  111.         for name, instance in dests.iteritems():
  112.             dest = None
  113.             if dest.is_default:
  114.                 return name
  115.         
  116.  
  117.     
  118.     def getSystemDefault(self):
  119.         
  120.         try:
  121.             return self.cups_connection.getDefault()
  122.         except:
  123.             pass
  124.  
  125.  
  126.  
  127.  
  128. class Dialog:
  129.     
  130.     def __init__(self):
  131.         gtk.window_set_default_icon_name('printer')
  132.         self.dialog = gtk.Dialog(_('Default Printer'), None, gtk.DIALOG_MODAL | gtk.DIALOG_NO_SEPARATOR, (gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE, _('_Use System Default'), gtk.RESPONSE_NO, _('_Set Default'), gtk.RESPONSE_OK))
  133.         self.dialog.set_default_response(gtk.RESPONSE_OK)
  134.         self.dialog.set_border_width(6)
  135.         self.dialog.vbox.set_spacing(2)
  136.         vbox = gtk.VBox(False, 6)
  137.         self.dialog.vbox.pack_start(vbox, True, True, 0)
  138.         vbox.set_border_width(6)
  139.         self.model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
  140.         self.model.set_sort_column_id(0, gtk.SORT_ASCENDING)
  141.         view = gtk.TreeView(self.model)
  142.         col = gtk.TreeViewColumn(_('Printer'), gtk.CellRendererText(), text = 0)
  143.         col.set_sort_column_id(0)
  144.         view.append_column(col)
  145.         col = gtk.TreeViewColumn(_('Location'), gtk.CellRendererText(), text = 1)
  146.         col.set_sort_column_id(1)
  147.         view.append_column(col)
  148.         self.view = view
  149.         view.get_selection().connect('changed', self.selection_changed)
  150.         scrollwin = gtk.ScrolledWindow()
  151.         scrollwin.set_shadow_type(gtk.SHADOW_IN)
  152.         scrollwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  153.         scrollwin.add(view)
  154.         vbox.pack_start(scrollwin, True, True, 0)
  155.         self.dialog.set_default_size(320, 240)
  156.         self.server = Server()
  157.         for button in self.dialog.action_area.get_children():
  158.             if button.get_label() == _('_Use System Default'):
  159.                 self.system_default_button = button
  160.                 continue
  161.             if button.get_label() == _('_Set Default'):
  162.                 self.set_default_button = button
  163.                 continue
  164.         
  165.  
  166.     
  167.     def selection_changed(self, selection):
  168.         (model, iter) = selection.get_selected()
  169.         if iter:
  170.             self.last_iter_selected = iter
  171.         else:
  172.             selection.select_iter(self.last_iter_selected)
  173.  
  174.     
  175.     def run(self):
  176.         c = cups.Connection()
  177.         printers = c.getPrinters()
  178.         del c
  179.         user_default = self.server.getUserDefault()
  180.         for name, info in printers.iteritems():
  181.             iter = self.model.append()
  182.             self.model.set_value(iter, 0, name)
  183.             self.model.set_value(iter, 1, info['printer-location'])
  184.             if name == user_default:
  185.                 self.view.get_selection().select_iter(iter)
  186.                 continue
  187.         
  188.         self.system_default_button.set_sensitive(self.server.isUserDefaultSet())
  189.         self.set_default_button.set_sensitive(len(printers) > 0)
  190.         self.dialog.show_all()
  191.         self.dialog.connect('response', self.response)
  192.         gtk.main()
  193.  
  194.     
  195.     def response(self, dialog, response):
  196.         if response == gtk.RESPONSE_CLOSE or response == gtk.RESPONSE_DELETE_EVENT:
  197.             gtk.main_quit()
  198.         
  199.         
  200.         try:
  201.             if response == gtk.RESPONSE_OK:
  202.                 (model, iter) = self.view.get_selection().get_selected()
  203.                 name = model.get_value(iter, 0)
  204.                 self.server.setUserDefault(name)
  205.                 self.system_default_button.set_sensitive(True)
  206.             elif response == gtk.RESPONSE_NO:
  207.                 self.server.clearUserDefault()
  208.                 system_default = self.server.getSystemDefault()
  209.                 iter = self.model.get_iter_first()
  210.                 while iter:
  211.                     name = self.model.get_value(iter, 0)
  212.                     if name == system_default:
  213.                         self.view.get_selection().select_iter(iter)
  214.                         break
  215.                     
  216.                     iter = self.model.iter_next(iter)
  217.                 self.system_default_button.set_sensitive(False)
  218.         except cups.IPPError:
  219.             (e, s) = None
  220.             if e == cups.IPP_SERVICE_UNAVAILABLE:
  221.                 error_out('<span weight="bold" size="larger">' + 'CUPS server error' + '</span>\n\n' + 'The CUPS scheduler is not running.')
  222.             else:
  223.                 error_out('<span weight="bold" size="larger">' + 'CUPS server error' + '</span>\n\n' + 'There was an error during the CUPS operation: ' + s)
  224.         except:
  225.             e == cups.IPP_SERVICE_UNAVAILABLE
  226.  
  227.  
  228.  
  229.  
  230. def error_out(msg):
  231.     d = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, '')
  232.     d.set_markup(msg)
  233.     d.run()
  234.     d.destroy()
  235.     sys.exit(1)
  236.  
  237.  
  238. try:
  239.     d = Dialog()
  240.     d.run()
  241. except RuntimeError:
  242.     error_out('<span weight="bold" size="larger">' + 'CUPS server error' + '</span>\n\n' + 'The CUPS scheduler is not running.')
  243. except cups.IPPError:
  244.     (e, s) = None
  245.     if e == cups.IPP_SERVICE_UNAVAILABLE:
  246.         error_out('<span weight="bold" size="larger">' + 'CUPS server error' + '</span>\n\n' + 'The CUPS scheduler is not running.')
  247.     else:
  248.         error_out('<span weight="bold" size="larger">' + 'CUPS server error' + '</span>\n\n' + 'There was an error during the CUPS operation: ' + s)
  249. except:
  250.     e == cups.IPP_SERVICE_UNAVAILABLE
  251.  
  252.